package com.bai.localshop.controller.user;

import com.bai.localshop.pojo.dto.SaveShoppingCartDTO;
import com.bai.localshop.pojo.entity.ShoppingCart;
import com.bai.localshop.pojo.vo.ShoppingCartVO;
import com.bai.localshop.result.Result;
import com.bai.localshop.service.ShoppingCartService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车控制器
 */

@RestController
@RequestMapping("/api/user/shoppingCart")
@RequiredArgsConstructor
public class ShoppingCartController {
    private final ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     */
    @PostMapping("/save")
    public Result<String> save(@RequestBody SaveShoppingCartDTO requestParam) {
        shoppingCartService.save(requestParam);
        return Result.success("添加购物车成功");
    }

    /**
     * 查看购物车
     */
    @GetMapping("/list")
    public Result<List<ShoppingCartVO>> queryList() {
        return Result.success(shoppingCartService.queryList());
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("/clean")
    public Result<String> cleanById() {
        shoppingCartService.cleanById();
        return Result.success("清空购物车成功");
    }

    /**
     * 删除购物车中一个商品（数量大于1就数量-1，等于1就删除商品）
     */
    @PostMapping("/reduce/{id}")
    public Result<String> reduceById(@PathVariable Long id){
        shoppingCartService.reduceById(id);
        return Result.success();
    }
    /**
     * 删除一条商品（无论数量为几，全部删除）
     */
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteById(@PathVariable Long id){
        shoppingCartService.deleteById(id);
        return Result.success("删除成功");
    }

}

